﻿using Moqikaka.Util.Attr;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Moqikaka.BLL
{
    /// <summary>
    /// 
    /// </summary>
    public class ActinInfo
    {
        /// <summary>
        /// 函数
        /// </summary>
        public MethodInfo method { get; set; }

        /// <summary>
        /// 参数类型
        /// </summary>
        public Type[] parameterType { get; set; }
    }
    /// <summary>
    /// 集合执行调用方法
    /// </summary>
    public class BLLExecuteActions
    {
        /// <summary>
        /// 
        /// </summary>
        private static readonly Dictionary<string, ActinInfo> allAction = new Dictionary<string, ActinInfo>();

        /// <summary>
        /// 初始化接口调用方法
        /// </summary>
        public static void Init()
        {

            Type[] types = Assembly.GetExecutingAssembly().GetTypes();
            string className = string.Empty;
            string actionName = string.Empty;
            //遍历所有对象
            foreach (var type in types)
            {
                //获取BLLAttribute标记的模块名
                var bllAttr = type.GetCustomAttribute<BLLAttribute>();
                if (bllAttr == null)
                    continue;
                if (string.IsNullOrEmpty(bllAttr.BLL_Name))
                    className = type.Name;
                else
                    className = bllAttr.BLL_Name;

                //获取BLLActionAttribute标记的函数名
                MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Static);
                if (methods != null)
                {
                    foreach (var method in methods)
                    {
                        var actionAttr = method.GetCustomAttribute<BLLActionAttribute>();
                        if (actionAttr != null)
                        {
                            if (string.IsNullOrEmpty(actionAttr.BLLAction_Name))
                                actionName = method.Name;
                            else
                                actionName = actionAttr.BLLAction_Name;


                            var parms = method.GetParameters();
                            Type[] parameterTypes = new Type[parms.Length];

                            for (Int32 i = 0; i < parms.Length; i++)
                                parameterTypes[i] = parms[i].ParameterType;

                            string key = string.Format("{0}_{1}", className, actionName);

                            if (allAction.ContainsKey(key))
                                throw new Exception("装载action出现重名函数，不允许函数重载！");
                            //存入缓存
                            allAction[string.Format("{0}_{1}", className, actionName)] = new ActinInfo()
                            {
                                method = method,
                                parameterType = parameterTypes
                            };
                        }
                    }
                }
            }

        }

        /// <summary>
        /// 获取某个函数
        /// </summary>
        /// <param name="className"></param>
        /// <param name="actionName"></param>
        /// <returns></returns>
        public static ActinInfo GetAction(string className, string actionName)
        {
            string key = string.Format("{0}_{1}", className, actionName);
            if (allAction.ContainsKey(key))
                return allAction[key];
            //没有找到返回为null
            return null;
        }
    }
}
